package com.yami.shop.user.comment.listener;

import com.yami.shop.bean.enums.ScoreLogType;
import com.yami.shop.bean.event.UserRegisterLogEvent;
import com.yami.shop.bean.model.UserExtension;
import com.yami.shop.user.comment.model.UserBalanceLog;
import com.yami.shop.user.comment.model.UserGrowthLog;
import com.yami.shop.user.comment.model.UserScoreDetail;
import com.yami.shop.user.comment.model.UserScoreLog;
import com.yami.shop.user.comment.service.UserBalanceLogService;
import com.yami.shop.user.comment.service.UserGrowthLogService;
import com.yami.shop.user.comment.service.UserScoreDetailService;
import com.yami.shop.user.comment.service.UserScoreLogService;
import lombok.AllArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.collections.CollectionUtils;
import org.springframework.context.event.EventListener;
import org.springframework.stereotype.Component;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.Objects;

/**
 * @author: cl
 * @date: 2021-04-21 11:09:06
 */
@Slf4j
@Component("userRegisterLogListener")
@AllArgsConstructor
public class UserRegisterLogListener {

    private final UserScoreDetailService userScoreDetailService;
    private final UserScoreLogService userScoreLogService;
    private final UserBalanceLogService userBalanceLogService;
    private final UserGrowthLogService userGrowthLogService;


    /**
     * 用户注册时，初始化 积分 余额 成长值 用户等级 添加日志
     */
    @EventListener(UserRegisterLogEvent.class)
    public void userRegisterLogListener(UserRegisterLogEvent event) {
        List<UserExtension> userExtensions = event.getUserExtensions();
        if (CollectionUtils.isEmpty(userExtensions)) {
            return;
        }
        // 积分日志
        List<UserScoreLog> scoreLogs = new ArrayList<>();
        // 积分明细
        List<UserScoreDetail> scoreDetails = new ArrayList<>();
        // 用户余额
        List<UserBalanceLog> userBalanceLogs = new ArrayList<>();
        // 成长值
        List<UserGrowthLog> growthLogs = new ArrayList<>();
        Date now = new Date();
        for (UserExtension userExtension : userExtensions) {
            String userId = userExtension.getUserId();
            // 积分log
            Integer score = userExtension.getScore();
            if (Objects.nonNull(score) && score > 0) {
                UserScoreLog userScoreLog = new UserScoreLog();
                userScoreLog.setUserId(userId);
                userScoreLog.setScore(score);
                // 用户批量导入注册设置积分和用户注册送积分，都归类为用户注册设置积分
                userScoreLog.setSource(ScoreLogType.REGISTER.value());
                userScoreLog.setCreateTime(now);
                userScoreLog.setIoType(1);
                scoreLogs.add(userScoreLog);
                // 积分明细
                UserScoreDetail addDetail = new UserScoreDetail();
                addDetail.setStatus(1);
                addDetail.setUserId(userId);
                addDetail.setCreateTime(now);
                addDetail.setUsableScore(score);
                scoreDetails.add(addDetail);
            }
            // 余额
            Double balance = userExtension.getBalance();
            if (Objects.nonNull(balance) && balance > 0) {
                UserBalanceLog userBalanceLog = new UserBalanceLog();
                userBalanceLog.setUserId(userId);
                userBalanceLog.setCreateTime(now);
                userBalanceLog.setChangeBalance(balance);
                userBalanceLog.setIoType(1);
                // 平台修改余额
                userBalanceLog.setType(5);
                userBalanceLogs.add(userBalanceLog);
            }
            // 成长值
            Integer growth = userExtension.getGrowth();
            if (Objects.nonNull(growth) && growth > 0) {
                UserGrowthLog growthLog = new UserGrowthLog();
                growthLog.setUserId(userId);
                growthLog.setSource(1);
                growthLog.setChangeGrowth(growth);
                growthLog.setCreateTime(now);
                growthLog.setRemarks("系统修改用户成长值");
                growthLogs.add(growthLog);
            }
        }

        if (CollectionUtils.isNotEmpty(scoreLogs)) {
            userScoreLogService.saveBatch(scoreLogs);
        }
        if (CollectionUtils.isNotEmpty(scoreDetails)) {
            userScoreDetailService.saveBatch(scoreDetails);
        }
        if (CollectionUtils.isNotEmpty(userBalanceLogs)) {
            userBalanceLogService.saveBatch(userBalanceLogs);
        }
        if (CollectionUtils.isNotEmpty(growthLogs)) {
            userGrowthLogService.saveBatch(growthLogs);
        }
    }

}
